//
//  LBCCategoryHomeVC.m
//  喜马拉雅改良版框架
//
//  Created by dllo on 15/12/24.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LBCCategoryHomeVC.h"

@interface LBCCategoryHomeVC ()<UITableViewDelegate, UITableViewDataSource, LBCCategoryHomeHeaderDelegate, LBCCategorySectionDelegate>

@property (nonatomic, strong)UITableView *homeTableView;
@property (nonatomic, strong)NSMutableArray *sectionArr;
@property (nonatomic, strong)NSMutableDictionary *topDic;
@property (nonatomic, strong)NSMutableArray *focusImageArr;

@end

@implementation LBCCategoryHomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.homeTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    self.homeTableView.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
    _homeTableView.delegate = self;
    _homeTableView.dataSource = self;
    [self.view addSubview:self.homeTableView];
    [_homeTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(@0);
        
    }];
    _homeTableView.rowHeight = SCREEN_HEIGHT / 7.4;
    
    // 去掉cell的线
    _homeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [_homeTableView registerClass:[LBCCategoryTopCell class] forCellReuseIdentifier:@"LBCCategoryTopCell"];
    [_homeTableView registerClass:[LBCCategoryListCell class] forCellReuseIdentifier:@"LBCCategoryListCell"];
    
    _sectionArr = [NSMutableArray array];
    _focusImageArr = [NSMutableArray array];
    [self getData];
    
    
}

- (void)getData{
    NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=%@&contentType=%@&device=iPhone&scale=2&version=4.3.26", _categoryId, _contentType];
    [AFNetworkTool getUrl:urlStr body:nil result:HXJSON headerFile:nil success:^(id result) {
        NSMutableDictionary *bigDic = [result objectForKey:@"categoryContents"];
        NSMutableArray *array = [bigDic objectForKey:@"list"];
        _topDic = [[array[0] objectForKey:@"list"] firstObject];
        for (NSMutableDictionary *dic in array) {
            LBCCateSectionModel *cateSectionM = [LBCCateSectionModel new];
            [cateSectionM setValuesForKeysWithDictionary:dic];
            [_sectionArr addObject:cateSectionM];
        }
        NSMutableDictionary *ImageDic = [result objectForKey:@"focusImages"];
        NSMutableArray *imageArr = [ImageDic objectForKey:@"list"];
        for (NSMutableDictionary *dic in imageArr) {
            LBCCategoryImageModel *imageM = [LBCCategoryImageModel new];
            [imageM setValuesForKeysWithDictionary:dic];
            [_focusImageArr addObject:imageM];
        }
        // 头视图
        LBCCategoryHomeHeaderV *cateHomeHV = [[LBCCategoryHomeHeaderV alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 3.7) array:_focusImageArr];
        cateHomeHV.categoryHomeHeaderDelegate = self;
        _homeTableView.tableHeaderView = cateHomeHV;
        [cateHomeHV.imageScrollV setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:YES];

        [_homeTableView reloadData];
    } failure:^(NSError *error) {
        
        
    }];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _sectionArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (0 == section) {
        return 1;
    }
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (0 == indexPath.section) {
        static NSString *idntifier = @"LBCCategoryTopCell";
        LBCCategoryTopCell *topCell = [tableView dequeueReusableCellWithIdentifier:idntifier];
        topCell.topDic = _topDic;
        // 显示cell右侧的箭头
        [topCell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        
        return topCell;
    } else {
        static NSString *idntifier = @"LBCCategoryListCell";
        LBCCategoryListCell *listCell = [tableView dequeueReusableCellWithIdentifier:idntifier];
        // 显示cell右侧的箭头
        [listCell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        if (_sectionArr.count != 0) {
            LBCCateSectionModel *sectionM = _sectionArr[indexPath.section];
            listCell.cateListModel = sectionM.list[indexPath.row];
        }
        
        return listCell;
    }
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LBCCategorySection *cateSection = [LBCCategorySection new];
    cateSection.index = section;
    cateSection.titleLabel.text = [_sectionArr[section] title];
    cateSection.categorySectionDelegate = self;
    // 计算文字大小
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    CGFloat length = [[_sectionArr[section] title] boundingRectWithSize:CGSizeMake(320, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
    [cateSection.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(length);
        
    }];

    return cateSection;
}

-(void)presentToAllViewControllerIndex:(NSInteger)index{
    LBCCategoryMoreVC *moreVC = [LBCCategoryMoreVC new];
    moreVC.categoryId = _categoryId;
    moreVC.tagName = [_sectionArr[index] tagName];
    [self.navigationController pushViewController:moreVC animated:YES];
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (0 == section) {
        return 0;
    }
    return SCREEN_HEIGHT / 16;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (0 == indexPath.section) {
        LBCCategoryTopVC *topVC = [LBCCategoryTopVC new];
        topVC.topDic = _topDic;
        [self.navigationController pushViewController:topVC animated:YES];
    } else {
        KBAlbumsDetailViewController *albumsVC = [KBAlbumsDetailViewController new];
        LBCCateSectionModel *sectionM = _sectionArr[indexPath.section];
        LBCCategoryListModel *cateListM = sectionM.list[indexPath.row];
        albumsVC.albumId = [NSString stringWithFormat:@"%ld", cateListM.albumId];
        [self.navigationController pushViewController:albumsVC animated:YES];
    }
}

-(void)presentToMusicPlayertagtrackId:(NSInteger)trackId{
    LXMusicPlayerVC *musicPlayerVC = [LXMusicPlayerVC new];
    LBCCategoryImageModel *imageM = _focusImageArr[trackId];
    musicPlayerVC.songId = imageM.trackId;
    musicPlayerVC.headImageLarge = imageM.pic;
    musicPlayerVC.Title = imageM.longTitle;
    [self presentViewController:musicPlayerVC animated:YES completion:^{
        
        
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
